package org.vectrics.recruiter.struts.admin;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.TreeSet;

import org.vectrics.common.pagelist.PageList;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.user.Person;
import org.vectrics.user.PersonEncounter;
import org.vectrics.user.PersonEncounterCriteria;
import org.vectrics.user.PersonEncounterDateComparator;

public class PersonEncountersPageList extends PageList {
	private Person person = null;
	private TreeSet encountersList = new TreeSet(new PersonEncounterDateComparator()); 
	
	
	public PersonEncountersPageList(Person person) {
		this.person = person;
		PersonEncounterCriteria criteria = new PersonEncounterCriteria();
		criteria.setPerson(person);
		log.info("Criteria: " + criteria);
		List encountersNotSorted = CoreServiceLocator.getPersonService().findPersonEncounters(criteria);
		encountersList.clear();
		encountersList.addAll(encountersNotSorted);
		log.info("Number of encounters (not sorted): " + encountersNotSorted.size());
		log.info("Number of encounters (sorted): " + encountersList.size());
		assert encountersList.size() == encountersNotSorted.size();
	}
	
	
	
	@Override
	public int getTotalElementCount() {
		return encountersList.size();
	}

	@Override
	public List getElements(int start, int end) {
		List list = new ArrayList();
		int index = 0;
		Iterator iter = this.encountersList.iterator();
		boolean done = false;
		if (iter.hasNext() == false) {
			done = true;
		}
		while ((iter.hasNext()) && (done == false)) {
			PersonEncounter encounter = (PersonEncounter)iter.next();
			if ((index >= start) && (index <= end)) {
				list.add(encounter);
			} else if (index > end) {
				done = true;
			}
			index++;
		}
		return list;
	}

}
